
package jfc.util;

import java.util.LinkedList;
import jfc.core.CardHolder;
import jfc.core.GameManager;

/**
 *  @author Yann Le Gall
 *  ylegall@gmail.com
 *  Oct 28, 2009 11:43:42 PM
 */
public class UndoManager
{
    private final LinkedList<MoveAction> actionList;
    private GameManager gameManager;

    public UndoManager(GameManager gameManager) {
        this.gameManager = gameManager;
        actionList = new LinkedList<MoveAction>();
    }
    
    public void clear() {actionList.clear();}

    public void undo() {
        if(!actionList.isEmpty()) {
            MoveAction ma = actionList.pop();
            gameManager.autoMove(ma.to, ma.from, ma.numCards, false);
        }
    }

    public void record(CardHolder from, CardHolder to, int num) {
        if(from != null && to != null) {
            MoveAction ma = new MoveAction();
            ma.from = from;
            ma.to = to;
            ma.numCards = num;
            actionList.push(ma);
        }
    }

    private class MoveAction {
        private CardHolder from, to;
        int numCards;
    }

}
